package misc

import (
	"jvm/instructions/base"
	"jvm/native"
	"jvm/rtda"
	"jvm/rtda/heap"
)

const smVM = "sun/misc/VM"

func init() {
	native.Register(smVM, "initialize", "()V", initialize)
}

// private static native void initialize();
func initialize(frame *rtda.Frame) {
	vmClass := frame.Method().Class()
	field := vmClass.GetField("savedProps", "Ljava/util/Properties;", true)
	savedProps := vmClass.StaticVars().GetRef(field.SlotId())
	key := heap.JString(vmClass.Loader(), "foo")
	val := heap.JString(vmClass.Loader(), "bar")
	frame.OPStack().PushRef(savedProps)
	frame.OPStack().PushRef(key)
	frame.OPStack().PushRef(val)
	propsClass := vmClass.Loader().LoadClass("java/util/Properties")
	setPropMethod := propsClass.GetInstanceMethod("setProperty",
		"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;")
	base.MethodInvoke(frame, setPropMethod)
}
